﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BizControl.Library.Security;

namespace BizControl.Presentation.Shell
{
  public partial class Login : Window
  {
    public Login()
    {
      InitializeComponent();
      this.UsernameTextBox.Focus();
    }

    void LoginButton(object sender, EventArgs e)
    {
      LoginLink.IsEnabled = false;
      if (Csla.ApplicationContext.User.Identity.IsAuthenticated)
        BizControlPrincipal.Logout();
      else
      {
        string username = UsernameTextBox.Text;
        string password = PasswordTextBox.Password;
        BizControlPrincipal.Login(username, password);
        if (!Csla.ApplicationContext.User.Identity.IsAuthenticated)
        {
          BizControlPrincipal.Logout();
          MessageBox.Show(BizControl.Presentation.Infrastructure.Properties.ErrorMessages.IncorrectLoginOrPassword,
            BizControl.Presentation.Infrastructure.Properties.Captions.CaptionError, MessageBoxButton.OK, MessageBoxImage.Error);
        }
        else
          this.Close();
      }
      LoginLink.IsEnabled = true;
    }

    void CancelButton(object sender, EventArgs e)
    {
      if (Csla.ApplicationContext.User.Identity.IsAuthenticated)
        BizControlPrincipal.Logout();
      this.Close();
    }

    //void UpdateButtonText()
    //{
    //  if (Csla.ApplicationContext.User.Identity.IsAuthenticated)
    //    LoginLink
    //  else
    //    LoginLink
    //}
  }
}
